package com.pz.cloud.framework.leagueMem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pz.cloud.framework.base.expection.CustomRuntimeException;
import com.pz.cloud.framework.business.leagueMem.entity.LeagueMem;
import com.pz.cloud.framework.business.leagueMem.service.ILeagueMemService;
import com.pz.cloud.framework.business.leagueMem.vo.EditByIdsReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.BaseController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 团员
 * @Author: jeecg-boot
 * @Date:   2020-11-28
 * @Version: V1.0
 */
@Api(tags="团员")
@RestController
@RequestMapping("/leagueMem")
@Slf4j
public class LeagueMemController extends BaseController<LeagueMem, ILeagueMemService> {
	@Autowired
	private ILeagueMemService leagueMemService;
	
	/**
	 * 分页列表查询
	 *
	 * @param leagueMem
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "团员-分页列表查询")
	@ApiOperation(value="团员-分页列表查询", notes="团员-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent = "league/LeagueMemList")
	public Result<?> queryPageList(LeagueMem leagueMem,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		IPage<LeagueMem> pageList = leagueMemService.findPage(leagueMem, req.getParameterMap());
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param leagueMem
	 * @return
	 */
	@AutoLog(value = "团员-添加")
	@ApiOperation(value="团员-添加", notes="团员-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody LeagueMem leagueMem) {
		if (leagueMemService.getByUserId(leagueMem.getUserId()) != null) {
			return Result.error("该团员已存在");
		}
		leagueMemService.save(leagueMem);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param leagueMem
	 * @return
	 */
	@AutoLog(value = "团员-编辑")
	@ApiOperation(value="团员-编辑", notes="团员-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody LeagueMem leagueMem) {
		leagueMemService.updateById(leagueMem);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "团员-通过id删除")
	@ApiOperation(value="团员-通过id删除", notes="团员-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		leagueMemService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "团员-批量删除")
	@ApiOperation(value="团员-批量删除", notes="团员-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.leagueMemService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "团员-通过id查询")
	@ApiOperation(value="团员-通过id查询", notes="团员-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		LeagueMem leagueMem = leagueMemService.getById(id);
		if(leagueMem==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(leagueMem);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param leagueMem
    */
    @RequestMapping(value = "/exportXls")
	@PermissionData(pageComponent = "league/LeagueMemList")
	public ModelAndView exportXls(HttpServletRequest request, LeagueMem leagueMem) {
		List<LeagueMem> list = leagueMemService.list(leagueMem, request.getParameterMap());
		return super.exportXls(request, list, LeagueMem.class, "团员");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LeagueMem.class);
    }

	 @AutoLog(value = "团员-批量修改团员信息")
	 @ApiOperation(value="团员-批量修改团员信息", notes="团员-批量修改团员信息")
	 @RequestMapping(value = "/editByIds", method = RequestMethod.POST)
	 public Result<?> editByIds(HttpServletRequest request, @RequestBody EditByIdsReq req) {
		 LambdaUpdateWrapper<LeagueMem> wrapper = Wrappers.lambdaUpdate();
		 wrapper.set(LeagueMem::getGraduateStatus, req.getGraduateStatus());
		 wrapper.in(LeagueMem::getId, req.getIds());
		 boolean update = service.update(wrapper);
		 return update ? Result.OK() : Result.error("更新失败");
	 }

}
